package com.test.timmy;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Person
	extends java.util.Observable
{
	public static final String TAG_PERSON_ID = "personID";

	private static final String TAG_NAME = "name";
	private static final String TAG_DEPARTMENT = "department";
	private static final String TAG_DESK_X = "desk_x";
	private static final String TAG_DESK_Y = "desk_y";
	private static final String TAG_MORALE = "morale";
	private static final String TAG_HUMOUR = "humour";
	private static final String TAG_SKILL = "skill";
	private static final String TAG_DAILY_CHANGE = "daily_change";

	private static final String UNDERSCORE = "_";
	
	private String name;
	private Bitmap photo;
	private String department;
	private int desk_x;
	private int desk_y;
	private int morale;
	private int humour;
	private int skill;
	private Changes dailyChanges;

	public Person(JSONObject details)
			throws JSONException
	{
		setName(details.getString(TAG_NAME));
		
		try {
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(details.getString("photo")).getContent());
			setPhoto(bitmap);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setDepartment(details.getString(TAG_DEPARTMENT));
		setDesk_x(Integer.valueOf(details.getString(TAG_DESK_X)));
		setDesk_y(Integer.valueOf(details.getString(TAG_DESK_Y)));
		setMorale(Integer.valueOf(details.getString(TAG_MORALE)));
		setHumour(Integer.valueOf(details.getString(TAG_HUMOUR)));
		setSkill(Integer.valueOf(details.getString(TAG_SKILL)));
		setDailyChanges(new Changes(details.getJSONObject(TAG_DAILY_CHANGE)));
	}

	public void reactToEvent(Changes changes)
			throws NumberFormatException, JSONException
	{
		setMorale(getMorale() + changes.getMorale());
		setHumour(getHumour() + changes.getHumour());
		setSkill(getSkill() + changes.getSkill());
	}
	
	//pseudo-unique
	public String getUniqueID()
	{
		return getName() + UNDERSCORE + getDepartment(); 
	}
	
	public String getName() {
		return name;
	}

	private void setName(String name) {
		this.name = name;
	}

	public void setPhoto(Bitmap photo)
	{
		this.photo = photo;
	}

	public Bitmap getPhoto() {
		return photo;
	}

	public String getDepartment() {
		return department;
	}

	private void setDepartment(String department) {
		this.department = department;
	}

	public int getDesk_x() {
		return desk_x;
	}

	public void setDesk_x(int desk_x) {
		this.desk_x = desk_x;
	}

	public int getDesk_y() {
		return desk_y;
	}

	public void setDesk_y(int desk_y) {
		this.desk_y = desk_y;
	}

	public int getMorale() {
		return morale;
	}

	public void setMorale(int morale) {
		this.morale = morale;
		hasChanged();
		notifyObservers();
	}

	public int getHumour() {
		return humour;
	}

	public void setHumour(int humour) {
		this.humour = humour;
		hasChanged();
		notifyObservers();
	}

	public int getSkill() {
		return skill;
	}

	public void setSkill(int skill) {
		this.skill = skill;
		hasChanged();
		notifyObservers();
	}

	public Changes getDailyChanges() {
		return dailyChanges;
	}

	public void setDailyChanges(Changes dailyChanges) {
		this.dailyChanges = dailyChanges;
	}
}
